package com.zone.core.binding

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.Log
import android.widget.LinearLayout
import androidx.annotation.LayoutRes
import androidx.databinding.BindingAdapter
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.zone.core.base.BaseBindingAdapter
import com.zone.core.base.adapter.DefaultBindingAdapter

/**
 *Author: last
 *Time: 2014-05-06
 *Description: xxx
 */

/**
 * RecycleView设置竖直分割线
 *
 * @param dividerSize 竖直分割线
 */
@BindingAdapter("verticalSpace")
fun setVerticalSpace(recyclerView: RecyclerView, dividerSize: Float) {
    val itemVDecoration = DividerItemDecoration(recyclerView.context, LinearLayout.VERTICAL)
    val drawable = GradientDrawable()
    drawable.setSize(0, dividerSize.toInt())
    drawable.setColor(Color.TRANSPARENT)
    itemVDecoration.setDrawable(drawable)
    recyclerView.addItemDecoration(itemVDecoration)
}

/**
 * RecycleView设置横向分割线
 *
 * @param dividerSize 横向分割线
 */
@BindingAdapter("horizontalSpace")
fun setHorizontalSpace(recyclerView: RecyclerView, dividerSize: Float) {
    val itemVDecoration = DividerItemDecoration(recyclerView.context, LinearLayout.HORIZONTAL)
    val drawable = GradientDrawable()
    drawable.setSize(dividerSize.toInt(), 0)
    drawable.setColor(Color.TRANSPARENT)
    itemVDecoration.setDrawable(drawable)
    recyclerView.addItemDecoration(itemVDecoration)
}

@BindingAdapter("itemLayout", "recyclerData", "itemEventHandler")
fun setItemLayout(
    recyclerView: RecyclerView,
    @LayoutRes itemLayout: Int,
    recyclerData: List<Any>?,
    itemEventHandler: Any?
) {
    setData(recyclerView, itemLayout, recyclerData, itemEventHandler, 1)
}

@BindingAdapter("itemLayout", "recyclerData", "itemEventHandler", "spanCount")
fun setData(
    recyclerView: RecyclerView,
    @LayoutRes itemLayout: Int,
    recyclerData: List<Any>?,
    itemEventHandler: Any?,
    spanCount: Int?
) {
    setData(recyclerView, itemLayout, recyclerData, null, null, itemEventHandler, spanCount)
}

@BindingAdapter("itemLayout", "recyclerData", "itemViewType", "spanCount")
fun setData(
    recyclerView: RecyclerView,
    @LayoutRes itemLayout: Int,
    data: List<Any>?,
    itemViewTypeCreator: BaseBindingAdapter.ItemViewTypeCreator?,
    spanCount: Int?
) {
    setData(recyclerView, itemLayout, data, null, itemViewTypeCreator, null, spanCount)
}

@BindingAdapter("itemLayout", "recyclerData", "itemClick", "spanCount")
fun setData(
    recyclerView: RecyclerView,
    @LayoutRes itemLayout: Int,
    data: List<Any>?,
    listener: BaseBindingAdapter.OnItemClickListener<Any>?,
    spanCount: Int?
) {
    setData(recyclerView, itemLayout, data, listener, null, null, spanCount)
}


@BindingAdapter("itemLayout", "recyclerData", "itemViewType", "itemEventHandler", "spanCount")
fun setData(
    recyclerView: RecyclerView,
    @LayoutRes itemLayout: Int,
    data: List<Any>?,
    itemViewTypeCreator: BaseBindingAdapter.ItemViewTypeCreator?,
    itemEventHandler: Any?,
    spanCount: Int?
) {
    setData(recyclerView, itemLayout, data, null, itemViewTypeCreator, itemEventHandler, spanCount)
}

@BindingAdapter("itemLayout", "recyclerData", "itemClick", "itemViewType", "spanCount")
fun setData(
    recyclerView: RecyclerView,
    @LayoutRes itemLayout: Int,
    data: List<Any>?,
    listener: BaseBindingAdapter.OnItemClickListener<Any>,
    itemViewTypeCreator: BaseBindingAdapter.ItemViewTypeCreator,
    spanCount: Int?
) {
    setData(recyclerView, itemLayout, data, listener, itemViewTypeCreator, null, spanCount)
}

@BindingAdapter("itemLayout", "recyclerData", "itemClick", "itemEventHandler", "spanCount")
fun setData(
    recyclerView: RecyclerView,
    @LayoutRes itemLayout: Int,
    data: List<Any>?,
    listener: BaseBindingAdapter.OnItemClickListener<Any>,
    itemEventHandler: Any?,
    spanCount: Int?
) {
    setData(recyclerView, itemLayout, data, listener, null, itemEventHandler, spanCount)
}

@BindingAdapter(
    "itemLayout",
    "recyclerData",
    "itemClick",
    "itemViewType",
    "itemEventHandler",
    "spanCount"
)
fun setData(
    recyclerView: RecyclerView,
    @LayoutRes itemLayout: Int,
    data: List<Any>?,
    listener: BaseBindingAdapter.OnItemClickListener<Any>?,
    itemViewTypeCreator: BaseBindingAdapter.ItemViewTypeCreator?,
    itemEventHandler: Any?,
    spanCount: Int?
) {
    val adapter = recyclerView.adapter
    if (adapter == null) {
        val simpleBindingAdapter = DefaultBindingAdapter(itemLayout)
        simpleBindingAdapter.data = data
        simpleBindingAdapter.itemClickListener = listener
        simpleBindingAdapter.itemViewTypeCreator = itemViewTypeCreator
        simpleBindingAdapter.itemEventHandler = itemEventHandler
        try {
            recyclerView.adapter = simpleBindingAdapter
            spanCount?.let {
                val layoutManager = recyclerView.layoutManager
                if (layoutManager is GridLayoutManager) {
                    layoutManager.spanCount = it
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    } else if (adapter is BaseBindingAdapter<*, *>) {
        (adapter as BaseBindingAdapter<Any, ViewDataBinding>).data = data
        adapter.itemViewTypeCreator = itemViewTypeCreator
        adapter.itemClickListener = listener
        adapter.itemEventHandler = itemEventHandler
    }
}